/*
 * Copyright © 2018 CODESTD.COM Inc. All rights reserved.
 */
package com.codestd.security.base;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @author jaune
 * @since 1.0.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ResponseBean {
    private static ObjectMapper objectMapper = new ObjectMapper();
    private boolean success;
    private int status;
    private String message;
    private Object data;

    public static class MapBuilder extends ResponseBeanBuilder {
        private Map<String, Object> dataMap = new HashMap<>();

        public MapBuilder add(String name, String value) {
            dataMap.put(name, value);
            return this;
        }

        public ResponseBean build() {
            this.data(dataMap);
            return super.build();
        }
    }

    public static MapBuilder mapBuilder() {
        return new MapBuilder();
    }

    public String asJson() {
        try {
            return objectMapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }

}
